import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../bloc/DraggableBloc.dart';

class DraggableFloatingCubitView extends StatelessWidget {

  final Widget child;
  const DraggableFloatingCubitView({ required this.child, super.key});

  @override
  Widget build(BuildContext context) {
    var draggableCubit = context.watch<DraggableCubit>();
    return Positioned(
        left: draggableCubit.state.leftPosition,
        top: draggableCubit.state.topPosition,
        child: Draggable<Widget>(
          child: child,
          feedback: child,
          childWhenDragging: Opacity(opacity: 0.5, child: Container()), // 拖动时的显示效果
          onDragUpdate: (details) {
            draggableCubit.add(DraggableMoveEvent(details.delta.dx, details.delta.dy));
          },
        ));
  }

}

class DraggableFloatingView extends StatefulWidget {
  
  final Widget child;
  const DraggableFloatingView({ required this.child, super.key});

  @override
  State<StatefulWidget> createState() => _DraggableFloatingViewState();
}

class _DraggableFloatingViewState extends State<DraggableFloatingView> {
  double _leftOffset = 200;
  double _topOffset = 100;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
        left: _leftOffset,
        top: _topOffset,
        child: Draggable<Widget>(
          child: widget.child,
          feedback: widget.child,
          childWhenDragging: Opacity(opacity: 0.5, child: Container()), // 拖动时的显示效果
          onDragUpdate: (details) {
            setState(() {
              _leftOffset += details.delta.dx;
              _topOffset += details.delta.dy;
            });
          },
        ));
  }
}
